<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>时钟</title>
</head>

<!-- onload 在页面加载完成时触发的事件 -->

<body onload="clockTime()">
    <script>
        function clockTime() {
            // new Date() 获取当前时间
            var todayDate = new Date();
            // 获取年份
            var year = todayDate.getFullYear();
            // 获取月份
            var month = todayDate.getMonth() + 1;
            // 获取日期
            var date = todayDate.getDate();
            // 获取时分秒
            var hour = todayDate.getHours();
            var minute = todayDate.getMinutes();
            var second = todayDate.getSeconds();
            // 展示的时间格式:2021-10-20 10:39:01
            var time = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
            // 1.得到div元素
            var obj = document.getElementById("dispalyTime");
            // 2.给div元素设置文本内容
            obj.innerHTML = time;
            // setTimeout 在指定的时间后调用函数
            setTimeout('clockTime()', 1000);
        }
    </script>
    <div id="dispalyTime">
    </div>
</body>

</html>